package com.qn.meta.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qn.common.base.BaseServiceImpl;
import com.qn.common.base.model.BasicFetchPage;
import com.qn.common.base.model.RequestPage;
import com.qn.core.utils.SaltMd5Util;
import com.qn.meta.entity.User;
import com.qn.meta.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
@DubboService(version = "1.0.0")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    @Autowired
    UserMapper  baseMapper;
    @Override
    public BasicFetchPage<User> getPage(RequestPage<User> rpage) {
        IPage<User> page = new Page<>(rpage.getPage(), rpage.getPageSize());
        IPage<User> pageList = baseMapper.getPage(page, rpage.getWhere());
        return new BasicFetchPage(pageList.getTotal(), pageList.getRecords());
    }

    @Override
    public void saves(User model) throws Exception {
        if (!Objects.isNull(model.getPassword())) {
            String md5=SaltMd5Util.md5(model.getPassword()) ;
            model.setPassword(SaltMd5Util.generateSaltPassword(md5));
        }
        if (Objects.isNull(model.getId())) {
            model.setNewUser(getUserName());
            model.setNewTime(new Date());
            model.setUpdataUser(getUserName());
            model.setUpdateTime(new Date());
            baseMapper.insert(model);
        } else {
            model.setUpdataUser(getUserName());
            model.setUpdateTime(new Date());
            baseMapper.updateById(model);
        }
    }

    @Override
    public void del(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public List<User> getList(User model) {
        return baseMapper.getList(model);
    }

    
    @Override
    public User getInfo() {
        User user = baseMapper.selectById(getLoginId());
        if (user != null) {
            user.setPassword(null);
        }
        return user;
    }


}